#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

#include "wrappedlibs.h"

#include "debug.h"
#include "wrapper.h"
#include "bridge.h"
#include "librarian/library_private.h"
#include "x86emu.h"
#include "emu/x86emu_private.h"
#include "callback.h"
#include "librarian.h"
#include "box86context.h"
#include "emu/x86emu_private.h"

const char* png12Name = "libpng12.so.0";
#define LIBNAME png12

#define ADDED_FUNCTIONS()           \

#include "generated/wrappedpng12types.h"

#include "wrappercallback.h"

#define SUPER() \
GO(0)   \
GO(1)   \
GO(2)   \
GO(3)

// user_write
#define GO(A)   \
static uintptr_t my_user_write_fct_##A = 0;                                         \
static void my_user_write_##A(void* png_ptr, void* data, int32_t length)            \
{                                                                                   \
    RunFunctionFmt(my_user_write_fct_##A, "ppi", png_ptr, data, length);\
}
SUPER()
#undef GO
static void* finduser_writeFct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_user_write_fct_##A == (uintptr_t)fct) return my_user_write_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_user_write_fct_##A == 0) {my_user_write_fct_##A = (uintptr_t)fct; return my_user_write_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libpng12 user_write callback\n");
    return NULL;
}
// user_flush
#define GO(A)   \
static uintptr_t my_user_flush_fct_##A = 0;                         \
static void my_user_flush_##A(void* png_ptr)                        \
{                                                                   \
    RunFunctionFmt(my_user_flush_fct_##A, "p", png_ptr);\
}
SUPER()
#undef GO
static void* finduser_flushFct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_user_flush_fct_##A == (uintptr_t)fct) return my_user_flush_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_user_flush_fct_##A == 0) {my_user_flush_fct_##A = (uintptr_t)fct; return my_user_flush_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libpng12 user_flush callback\n");
    return NULL;
}
// user_read
#define GO(A)   \
static uintptr_t my_user_read_fct_##A = 0;                                          \
static void my_user_read_##A(void* png_ptr, void* data, int32_t length)             \
{                                                                                   \
    RunFunctionFmt(my_user_read_fct_##A, "ppi", png_ptr, data, length); \
}
SUPER()
#undef GO
static void* finduser_readFct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_user_read_fct_##A == (uintptr_t)fct) return my_user_read_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_user_read_fct_##A == 0) {my_user_read_fct_##A = (uintptr_t)fct; return my_user_read_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libpng12 user_read callback\n");
    return NULL;
}
// error
#define GO(A)   \
static uintptr_t my_error_fct_##A = 0;                          \
static void my_error_##A(void* a, void* b)                      \
{                                                               \
    RunFunctionFmt(my_error_fct_##A, "pp", a, b);   \
}
SUPER()
#undef GO
static void* finderrorFct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_error_fct_##A == (uintptr_t)fct) return my_error_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_error_fct_##A == 0) {my_error_fct_##A = (uintptr_t)fct; return my_error_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libpng12 error callback\n");
    return NULL;
}
// warning
#define GO(A)   \
static uintptr_t my_warning_fct_##A = 0;                        \
static void my_warning_##A(void* a, void* b)                    \
{                                                               \
    RunFunctionFmt(my_warning_fct_##A, "pp", a, b); \
}
SUPER()
#undef GO
static void* findwarningFct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_warning_fct_##A == (uintptr_t)fct) return my_warning_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_warning_fct_##A == 0) {my_warning_fct_##A = (uintptr_t)fct; return my_warning_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libpng12 warning callback\n");
    return NULL;
}
// malloc
#define GO(A)   \
static uintptr_t my_malloc_fct_##A = 0;                         \
static void my_malloc_##A(void* a, unsigned long b)             \
{                                                               \
    RunFunctionFmt(my_malloc_fct_##A, "pL", a, b);  \
}
SUPER()
#undef GO
static void* findmallocFct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_malloc_fct_##A == (uintptr_t)fct) return my_malloc_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_malloc_fct_##A == 0) {my_malloc_fct_##A = (uintptr_t)fct; return my_malloc_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libpng12 malloc callback\n");
    return NULL;
}
// free
#define GO(A)   \
static uintptr_t my_free_fct_##A = 0;                       \
static void my_free_##A(void* a, void* b)                   \
{                                                           \
    RunFunctionFmt(my_free_fct_##A, "pp", a, b);\
}
SUPER()
#undef GO
static void* findfreeFct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_free_fct_##A == (uintptr_t)fct) return my_free_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_free_fct_##A == 0) {my_free_fct_##A = (uintptr_t)fct; return my_free_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libpng12 free callback\n");
    return NULL;
}

// progressive_info
#define GO(A)   \
static uintptr_t my_progressive_info_fct_##A = 0;                       \
static void my_progressive_info_##A(void* a, void* b)                   \
{                                                                       \
    RunFunctionFmt(my_progressive_info_fct_##A, "pp", a, b);\
}
SUPER()
#undef GO
static void* findprogressive_infoFct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_progressive_info_fct_##A == (uintptr_t)fct) return my_progressive_info_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_progressive_info_fct_##A == 0) {my_progressive_info_fct_##A = (uintptr_t)fct; return my_progressive_info_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libpng12 progressive_info callback\n");
    return NULL;
}

// progressive_end
#define GO(A)   \
static uintptr_t my_progressive_end_fct_##A = 0;                        \
static void my_progressive_end_##A(void* a, void* b)                    \
{                                                                       \
    RunFunctionFmt(my_progressive_end_fct_##A, "pp", a, b); \
}
SUPER()
#undef GO
static void* findprogressive_endFct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_progressive_end_fct_##A == (uintptr_t)fct) return my_progressive_end_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_progressive_end_fct_##A == 0) {my_progressive_end_fct_##A = (uintptr_t)fct; return my_progressive_end_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libpng12 progressive_end callback\n");
    return NULL;
}

// progressive_row
#define GO(A)   \
static uintptr_t my_progressive_row_fct_##A = 0;                                \
static void my_progressive_row_##A(void* a, void* b, uint32_t c, int d)         \
{                                                                               \
    RunFunctionFmt(my_progressive_row_fct_##A, "ppui", a, b, c, d); \
}
SUPER()
#undef GO
static void* findprogressive_rowFct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_progressive_row_fct_##A == (uintptr_t)fct) return my_progressive_row_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_progressive_row_fct_##A == 0) {my_progressive_row_fct_##A = (uintptr_t)fct; return my_progressive_row_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libpng12 progressive_row callback\n");
    return NULL;
}

#undef SUPER

EXPORT void my12_png_set_write_fn(x86emu_t* emu, void* png_ptr, void* ioptr, void* write_fn, void* flush_fn)
{
    (void)emu;
    my->png_set_write_fn(png_ptr, ioptr, finduser_writeFct(write_fn), finduser_flushFct(flush_fn));
}

EXPORT void my12_png_set_read_fn(x86emu_t* emu, void* png_ptr, void* ioptr, void* read_fn)
{
    (void)emu;
    my->png_set_read_fn(png_ptr, ioptr, finduser_readFct(read_fn));
}

EXPORT void my12_png_set_error_fn(x86emu_t* emu, void* pngptr, void* errorptr, void* error_fn, void* warning_fn)
{
    (void)emu;
    my->png_set_error_fn(pngptr, errorptr, finderrorFct(error_fn), findwarningFct(warning_fn));
}

EXPORT void* my12_png_create_read_struct_2(x86emu_t* emu, void* user_png_ver, void* error_ptr, void* error_fn, void* warn_fn, void* mem_ptr, void* malloc_fn, void* free_fn)
{
    (void)emu;
    return my->png_create_read_struct_2(user_png_ver, error_ptr, finderrorFct(error_fn), findwarningFct(warn_fn), mem_ptr, findmallocFct(malloc_fn), findfreeFct(free_fn));
}

EXPORT void* my12_png_create_write_struct_2(x86emu_t* emu, void* user_png_ver, void* error_ptr, void* error_fn, void* warn_fn, void* mem_ptr, void* malloc_fn, void* free_fn)
{
    (void)emu;
    return my->png_create_write_struct_2(user_png_ver, error_ptr, finderrorFct(error_fn), findwarningFct(warn_fn), mem_ptr, findmallocFct(malloc_fn), findfreeFct(free_fn));
}

EXPORT void my12_png_set_progressive_read_fn(x86emu_t* emu, void* png_ptr, void* user_ptr, void* info, void* row, void* end)
{
    (void)emu;
    my->png_set_progressive_read_fn(png_ptr, user_ptr, findprogressive_infoFct(info), findprogressive_rowFct(row), findprogressive_endFct(end));
}

EXPORT void* my12_png_create_read_struct(x86emu_t* emu, void* png_ptr, void* user_ptr, void* errorfn, void* warnfn)
{
    (void)emu;
    return my->png_create_read_struct(png_ptr, user_ptr, finderrorFct(errorfn), findwarningFct(warnfn));
}

// Maybe this is needed?
//#define CUSTOM_INIT     lib->priv.w.altprefix=strdup("yes");

#define CUSTOM_INIT \
    getMy(lib);   \
    SETALT(my12_);

#define CUSTOM_FINI \
    freeMy();

#include "wrappedlib_init.h"
